package com.forever7776.life.core.service.impl.comment;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.cache.RedisCacheOperator;
import com.forever7776.life.core.common.constants.GlobalConstants;
import com.forever7776.life.core.common.constants.RedisCacheWebConstants;
import com.forever7776.life.core.common.vo.UserCommentVO;
import com.forever7776.life.core.entity.comment.CommentTweet;
import com.forever7776.life.core.mapper.comment.CommentTweetMapper;
import com.forever7776.life.core.service.comment.ICommentTweetService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 动弹评论表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-04-08
 */
@Service
public class CommentTweetServiceImpl extends ServiceImpl<CommentTweetMapper, CommentTweet> implements ICommentTweetService {

    @Autowired
    private CommentTweetMapper commentTweetMapper;
    @Autowired
    private RedisCacheOperator redisCacheOperator;

    /**
     * 查询用户ID，以构造用户基础信息数据map集合
     *
     * @param userId
     * @return
     */
    @Override
    public List<String> selectUserIds(String userId) {
        //life:web:tweet:userId:users
        String key = RedisCacheWebConstants.TWEET_USER + userId + GlobalConstants.SEPARATOR_COLON + GlobalConstants.USERS;
        List<String> userIds = redisCacheOperator.getCacheList(key);
        if (CollectionUtils.isNotEmpty(userIds)) {
            return userIds;
        }
        userIds = commentTweetMapper.selectUserIds(userId);
        redisCacheOperator.setCacheList(key, userIds, 1, TimeUnit.HOURS);
        return userIds;
    }

    /**
     * 根据动弹ID查询评论用户ID，以构造用户基础信息数据map集合
     *
     * @param tweetId
     * @return
     */
    @Override
    public List<String> selectUserIdsById(Integer tweetId) {
        //life:web:tweet:tweetId:users
        String key = RedisCacheWebConstants.TWEET_USER + tweetId + GlobalConstants.SEPARATOR_COLON + GlobalConstants.USERS;
        List<String> userIds = redisCacheOperator.getCacheList(key);
        if (CollectionUtils.isNotEmpty(userIds)) {
            return userIds;
        }
        userIds = commentTweetMapper.selectUserIdsById(tweetId);
        redisCacheOperator.setCacheList(key, userIds, 1, TimeUnit.HOURS);
        return userIds;
    }

    /**
     * 查询动弹评论列表
     *
     * @param userId
     * @param tweetId
     * @return
     */
    @Override
    public Page<UserCommentVO> selectUserCommentsPage(String userId, Integer tweetId) {
        Page<UserCommentVO> page = new Page<>();
        List<UserCommentVO> list = commentTweetMapper.selectUserCommentList(userId, tweetId);
        page.setRecords(list);
        return page;
    }

    /**
     * 查询动弹评论列表
     *
     * @param tweetId
     * @param userId
     * @return
     */
    @Override
    public List<UserCommentVO> selectUserCommentsList(String userId, Integer tweetId) {
        return commentTweetMapper.selectUserCommentList(userId, tweetId);
    }

}
